﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;

namespace ApplicationSecurityService
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1;
        System.Timers.Timer timer2;
        System.Timers.Timer timer3;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            LogHelper.Instance.Init();
            ConfigHelper.Instance.Init();
            ProcessManager.Instance.Init();

            timer1 = new System.Timers.Timer();
            timer1.Interval = 120000;  //设置计时器事件间隔执行时间 120秒
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
            timer1.Enabled = true;

            timer2 = new System.Timers.Timer();
            timer2.Interval = 1800000;  //设置计时器事件间隔执行时间 1800秒
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
            timer2.Enabled = true;

            timer3 = new System.Timers.Timer();
            timer3.Interval = 30000;  //设置计时器事件间隔执行时间 30秒
            timer3.Elapsed += new System.Timers.ElapsedEventHandler(timer3_Elapsed);
            timer3.Enabled = true;
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            if (Interop.dProcessID != 0)
            {
                Process ps = Process.GetProcessById(Interop.dProcessID);
                try
                {
                    ps.Kill();
                }
                catch { }
            }
        }

        // 测试日志,可以去除
        public void WriteLog()
        {
            try
            {
                String strPath = "D:\\C#Project\\ApplicationSecurityService\\log\\";
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                string strFilePath = strPath + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                File.AppendAllText(strFilePath, DateTime.Now.ToString("HH:mm:ss") + "\r\n");
            }
            catch { }
        }

        // 执行定时进程扫描,比对MD5码
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // 测试日志,可以去除,WriteLog()这个方法可以不调用,删除即可
            WriteLog();
            ProcessManager.Instance.ProcessSecurity();
        }

        // 执行定时配置更新,以及从服务器获取扫描间隔时间
        private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ConfigHelper.Instance.DownloadConfig();
            String sJsonConfig = ProcessManager.Instance.RequestConfig();
            if(sJsonConfig != "")
            {
                try
                {
                    JObject jo = (JObject)JsonConvert.DeserializeObject(sJsonConfig);
                    Int32 iSecurityTime = int.Parse(jo["SecurityTime"].ToString());
                    if (iSecurityTime > 30000 && iSecurityTime != timer1.Interval)
                    {
                        timer1.Interval = iSecurityTime;
                    }
                }
                catch { }
            }
        }

        // Security程序监控,如果程序被关了,那么启动它
        private void timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Process[] pCheckProecss = Process.GetProcessesByName("Security");
            if(pCheckProecss.Length == 0)
            {
                string ApplicationSecurityPath = ConfigHelper.Instance.AppFilePath + "Security.exe";
                Interop.CreateProcess(ApplicationSecurityPath, ConfigHelper.Instance.AppFilePath);
            }
        }
    }
}
